package com.corx.network.config;


import com.corx.network.config.condition.MissingMySqlMessageStorageCondition;
import com.corx.network.core.load.NetworkLoad;
import com.corx.network.core.manager.NetworkManager;
import com.corx.network.core.manager.ReceivedMessageListenerJarManager;
import com.corx.network.core.storage.Storage;
import com.corx.network.mysqlstorage.DefaultMySqlStorage;
import com.corx.network.mysqlstorage.hub.DeviceRepositoryHub;
import com.corx.network.mysqlstorage.load.JdbcNetworkLoad;
import com.corx.network.mysqlstorage.repository.*;
import com.corx.network.mysqlstorage.util.TableNameGeneratorUtil;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @author gx
 * @version 1.0.0
 */
@Configuration
@ConditionalOnClass(JdbcTemplate.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class StorageAutoConfiguration {


    @Bean
    @ConditionalOnMissingBean
    public DeviceRepositoryHub deviceRepositoryHub(JdbcTemplate jdbcTemplate) {
        return new DeviceRepositoryHub(
            new JdbcDeviceModelBindRepository(jdbcTemplate),
            new JdbcDeviceModelRepository(jdbcTemplate),
            new JdbcDeviceDataRawRepository(jdbcTemplate),
            new JdbcDeviceModelFieldRepository(jdbcTemplate),
            new JdbcDeviceDataStructuredRepository(jdbcTemplate),
            new JdbcDeviceRepository(jdbcTemplate),
            new JdbcDevicePointRepository(jdbcTemplate),
            new JdbcNetworkRepository(jdbcTemplate)
        );
    }

    @Bean
    @ConditionalOnMissingBean
    public DatabaseInitializer databaseInitializer(JdbcTemplate jdbcTemplate) {
        return new JdbcDatabaseInitializer(jdbcTemplate);
    }

    @Bean
    @ConditionalOnMissingBean
    public NetworkLoad networkLoad(
        ReceivedMessageListenerJarManager jarManager,
        NetworkManager networkManager,
        DeviceRepositoryHub hub
    ) {
        return new JdbcNetworkLoad(jarManager, networkManager, hub);
    }

    @Bean
    @ConditionalOnBean({DeviceRepositoryHub.class, DatabaseInitializer.class, NetworkLoad.class})
    @Conditional(MissingMySqlMessageStorageCondition.class)
    public Storage storage(
        DeviceRepositoryHub hub,
        DatabaseInitializer initializer,
        NetworkLoad networkLoad
    ) {
        initializer.init();
        networkLoad.initNetworks();
        return new DefaultMySqlStorage(hub, TableNameGeneratorUtil.TimeGranularity.NONE);
    }
}





